﻿using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace Utils
{
    public class ValueParser
    {
        private enum ReadState
        {
            TypeName,
            Args,
        }
        public string TypeName { get; private set; }
        public string[] Args { get; private set; }
        public ValueParser(string definitionString)
        {
            var state = ReadState.TypeName;
            var buffer = new StringBuilder();
            var brackets = 0;
            for (int i = 0; i < definitionString.Length; i++)
            {
                char current = definitionString[i];
                switch (state)
                {
                    case ReadState.TypeName:
                        if (current == '(')
                        {
                            TypeName = buffer.ToString();
                            buffer.Clear();
                            brackets += 1;
                            state = ReadState.Args;
                            continue;
                        }

                        if (!char.IsWhiteSpace(current))
                        {
                            buffer.Append(current);
                        }
                        break;

                    case ReadState.Args:
                        if (current == '(')
                        {
                            brackets += 1;
                        }

                        if (current == ')')
                        {
                            if (brackets == 1)
                            {
                                Args = buffer.ToString().Split(',');
                                return;
                            }
                            brackets -= 1;
                        }

                        if (!char.IsWhiteSpace(current))
                        {
                            buffer.Append(current);
                        }
                        break;
                }
            }
        }
    }
}
